package com.eduspace.eduplatform.wares.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author tbc on 2016/11/21 09:12:42.
 */
@EnableWebMvc
@Configuration
//@ImportResource("config.properties")
@ImportResource
@ComponentScan({
        "com.eduspace.eduplatform.wares.controller",
        "com.eduspace.eduplatform.wares.exception",
        "com.eduspace.eduplatform.util.web.controller"
})
public class SpringMVCConfiguration extends WebMvcConfigurerAdapter {

    @Value("${yijiajiao.mappingjackson.camelCaseToLowerCaseWithUnderscores:false}")
    private boolean camelCaseToLowerCaseWithUnderscores;
    @Value("${yijiajiao.mappingjackson.isIndent:false}")
    private boolean isIndent;
    @Value("${yijiajiao.mappingjackson.isOutputNull:false}")
    private boolean isOutputNull;
    @Value("${yijiajiao.mappingjackson.dateFormatPattern:yyyy-MM-dd HH:mm:ss}")
    private String dateFormatPattern;


    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> httpMessageConverter : converters) {
            if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
                converterObjectMapper((MappingJackson2HttpMessageConverter) httpMessageConverter);
            }
        }
    }

    private void converterObjectMapper(MappingJackson2HttpMessageConverter httpMessageConverter) {
        ObjectMapper om = httpMessageConverter.getObjectMapper();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//                    json2.setJsonPrefix("xxx");
        // 排除值为空属性
        if (isOutputNull) {
            om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        }
        // 进行缩进输出
        if (isIndent) {
            om.configure(SerializationFeature.INDENT_OUTPUT, true);
        }
        // 将驼峰转为下划线
        if (camelCaseToLowerCaseWithUnderscores) {
            om.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        }
        // 进行日期格式化
        DateFormat dateFormat = new SimpleDateFormat(dateFormatPattern);
        om.setDateFormat(dateFormat);
    }

    /**
     * 添加静态资源swagger-ui.html，否则请求不到页面
     *
     * @param registry .
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/druid/*").addResourceLocations("classpath:/META-INF/resources/");
    }

}